package router

import (
	"log"

	platformController "gitee.com/wangshh_123/cross-pay-go/src/controller/platform"
	controller "gitee.com/wangshh_123/cross-pay-go/src/controller/system"
	"gitee.com/wangshh_123/cross-pay-go/src/middleware"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/hlPay"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/photonpay"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// SetupUserRouter 设置用户相关路由
func SetupUserRouter(router *gin.RouterGroup, db *gorm.DB, photonPayClient *photonpay.PhotonPayClient, hlPayClient *hlPay.HlPayClient) {
	// 创建 CardListController 实例
	cardListController := platformController.NewCardListController(photonPayClient, hlPayClient, db)

	// 创建用户控制器
	userController := controller.NewUserController(db, photonPayClient, cardListController)
	authController, err := controller.NewAuthController()
	if err != nil {
		log.Fatalf("Failed to create auth controller: %v", err)
	}

	// 用户认证相关路由
	auth := router.Group("/auth")
	{

		// 两步登录相关路由
		auth.POST("/pre-login", authController.PreLogin)
		auth.POST("/send-login-code", authController.SendLoginVerificationCode)
		auth.POST("/verify-and-login", authController.VerifyAndLogin)

		// 注册相关路由
		auth.POST("/register", userController.Register)                               // 用户注册（宽松验证）
		auth.POST("/send-register-code", authController.SendRegisterVerificationCode) // 发送注册验证码

		// 密码相关路由
		auth.POST("/forgot-password/email-code", userController.SendForgotPasswordEmailCode)
		auth.POST("/forgot-password/sms-code", userController.SendForgotPasswordSmsCode)
		auth.POST("/forgot-password/reset", userController.ResetPassword)

		// 其他认证路由
		auth.POST("/logout", middleware.AuthMiddleware(), userController.Logout)
	}

	// 用户信息相关路由
	users := router.Group("/users")
	users.Use(middleware.AuthMiddleware())
	{
		// 用户基本信息
		users.GET("/profile", userController.GetProfile)      // 获取个人信息
		users.PUT("/profile", userController.UpdateProfile)   // 修改个人信息
		users.PUT("/password", userController.ChangePassword) // 修改密码
		users.GET("/info", userController.GetUserInfo)        // 获取用户详细信息

		// 认证方式相关路由
		users.GET("/auth-methods", userController.GetUserAuthMethods)    // 获取认证方式
		users.POST("/auth-methods", userController.CreateUserAuthMethod) // 创建认证方式
		users.PUT("/auth-methods", userController.UpdateUserAuthMethod)  // 更新认证方式
		users.GET("/login-records", userController.GetUserLoginRecords)  // 获取登录记录

		// 谷歌验证器相关路由
		users.GET("/google-auth/qrcode", authController.GetGoogleAuthenticatorQRCode) // 获取谷歌验证器二维码
		users.POST("/google-auth/bind", authController.BindGoogleAuthenticator)       // 绑定谷歌验证器
		users.POST("/google-auth/unbind", authController.UnbindGoogleAuthenticator)   // 解绑谷歌验证器
		users.POST("/google-auth/verify", authController.VerifyGoogleAuthenticator)   // 验证谷歌验证器
	}

	// 管理员接口路由
	admin := users.Group("/admin")
	admin.Use(middleware.AdminMiddleware())
	{
		admin.GET("", userController.ListUsers)            // 获取用户列表
		admin.GET("/:id", userController.GetUser)          // 获取指定用户信息
		admin.GET("/:id/info", userController.GetUserInfo) // 获取指定用户详细信息
		admin.PUT("/:id", userController.UpdateUser)       // 更新指定用户信息
		admin.DELETE("/:id", userController.DeleteUser)    // 删除用户
	}
}
